package edu.xjnu.uitls;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;
import java.util.List;

// 演示例子，执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {
    private final static String url = "jdbc:mysql://localhost:3306/itheima?useSSL=False&serverTimezone=UTC&allowMultiQueries=true";
    private final static String username = "root";
    private final static String password = "123456";
    private final static String basePath = "D:\\WorkSpace\\IdeaWorkspace\\MavenProject\\SpringBootTestDemo\\src\\main";
    private final static String outputDir = basePath + "\\java\\";
    private final static String pathInfo = basePath + "\\resources\\edu\\xjnu\\";

    public static void main(String[] args) {

        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author("David") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(outputDir); // 指定输出目录，注意：这里需要使用绝对路径
                })
                .packageConfig(builder -> {
                    builder.parent("edu.xjnu") // 设置父包名
                            //.moduleName("system") // 设置父包模块名
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .xml("mapper.xml")
                            .controller("controller")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, pathInfo)); // 设置mapperXml生成路径, 注意：这里需要使用绝对路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(List.of("user", "song", "review", "music", "user_music")) // 设置需要生成的表名
                            .addTablePrefix("t_", "c_"); // 设置过滤表前缀

                    // 实体类生成配置
                    builder.entityBuilder()
                            .disableSerialVersionUID()
                            .enableLombok()
                            .enableRemoveIsPrefix()
                            .enableTableFieldAnnotation()
                            .enableActiveRecord()
                            .formatFileName("%sEntity")
                            .build();

                    // Controller 生成策略配置
                    builder.controllerBuilder()
                            .enableHyphenStyle()
                            .enableRestStyle()
                            .formatFileName("%sController")
                            .build();

                    // Service 生成策略配置
                    builder.serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .build();

                    // mapper 生成策略配置
                    builder.mapperBuilder()
                            .superClass(MyBaseMapper.class)
                            .enableMapperAnnotation()
                            .enableBaseResultMap()
                            .enableBaseColumnList()
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sXml")
                            .build();
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
